<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    incomeType:{
      type:Array,
      default: []
    },
    expenseType:{
      type: Array,
      default: []
    },
    title:{
      type: String,
      default: '2023年8月支出情况表'
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch:{
    expenseType:{
      deep:true,
      handler(val){
        this.initChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title:{
          show: true,
          text: this.title,
          x: "center",
          y: "bottom"
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '40',
          data: ['工资', '运费', '材料', '汽车', '租金']
        },
        series: [
          {
            name: '支出情况',
            type: 'pie',
            // roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: this.expenseType[0], name: '工资' },
              { value: this.expenseType[1], name: '运费' },
              { value: this.expenseType[2], name: '材料' },
              { value: this.expenseType[3], name: '汽车' },
              { value: this.expenseType[4], name: '租金' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
